package com.meessiom.train.crm.refund.service;

import com.meessiom.train.crm.common.exception.ResourceNotFoundException;
import com.meessiom.train.crm.refund.model.Refund;
import com.meessiom.train.crm.refund.repository.RefundRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class RefundService {
    @Autowired
    private RefundRepository refundRepository;

    public List<Refund> getAllRefunds(){
        return refundRepository.findAll();
    }
    public Refund createRefund(Refund refund){
        return refundRepository.save(refund);
    }

    public Refund getRefundById(long id){
        Refund refund = refundRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Refund not exist with id"  + id));
        return refund;
    }

    public List<Refund> saveAll(List<Refund> os){
        return refundRepository.saveAll(os);
    }

    public Refund deleteRefund(long id){
        Refund refund = refundRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Refund not exist with id"  + id));
        refundRepository.deleteById(id);
        return refund;
    }

    public Refund updateRefund(long id, Refund refundDetails){
        Refund updateRefund = refundRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Refund not exist with id"  + id));
        updateRefund.setRefundNumber(refundDetails.getRefundNumber());
        updateRefund.setRelatedCustomer(refundDetails.getRelatedCustomer());
        updateRefund.setRelatedOrder(refundDetails.getRelatedOrder());
        updateRefund.setRefundAmount(refundDetails.getRefundAmount());
        updateRefund.setRefundMethod(refundDetails.getRefundMethod());
        updateRefund.setRefundDate(refundDetails.getRefundDate());
        updateRefund.setApprovalStatus(refundDetails.getApprovalStatus());
        updateRefund.setResponsiblePerson(refundDetails.getResponsiblePerson());
        refundRepository.save(updateRefund);
        return updateRefund;
    }
}